<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/1/6
 * Time: 上午12:13
 */

namespace Home\Model;
use Think\Model;

class UserModel extends Model{

    protected $tableName = 'user';
    protected $pk         = 'user_id';
    protected $_validate = array(
        //验证手机号码
        array('phone', '', '手机已注册！', 1, 'unique', 3),
        array('phone','require','必须填写手机号！','1'),
        array('phone','_checkPhone','手机号码错误',1,'function'),
        //验证手机验证码
        array('verficode','require','必须填写手机验证码','1'),
        array('verficode','_checkVerfiCode','验证码错误',1,'function'),
        //确认密码
        array('passwd','require','必须填写密码！',1),
        array('passwd','confirm_passwd','两次密码输入不同!',1,'confirm'),
        //确认性别
        array('sex','require','请选择性别',1),

    );

    //判断手机号格式正确
    public static function _checkPhone($phone){
        return (preg_match("/^1[34578]{1}\d{9}$/",$phone));
    }
    //判断验证码是否输入正确
    public static function _checkVerfiCode($verficode){
        if($verficode == $_SESSION['verficode']){
            return true;
        }else{
            return false;
        }
    }

    //判断手机是否已注册
    public function isPhoneRegister($phone){
        $user_map['phone'] = array('eq',$phone);
        $user_result = $this->where($user_map)->select();
        if(is_array($user_result)){
            return true;
        }else{
            return false;
        }
    }

    //用户注册
    public function toRegister($phone,$passwd,$user_name,$sex){
        $data['user_name'] = $user_name;
        $data['sex'] = $sex;
        $data['passwd'] = $passwd;
        $data['phone'] = $phone;
        $data['status'] = 1;
        if ($sex == 2)
            $data['image_url'] = "default_avatar_female.png";
        else
            $data['image_url'] = "default_avatar.png";
        $user_reuslt = $this->add($data);
        //dump($user_reuslt);
        if($user_reuslt){
            $user_ext_data['trust'] = 1;
            $user_ext_data['level_j'] = 1;
            $user_ext_data['level_y'] = 1;
            $user_ext_data['ispublic_degree'] = 2;
            $user_ext_data['ispublic_birthday'] = 2 ;
            $user_ext_data['ispublic_phone'] = 2;
            $user_ext_data['duration'] = '1,2,3,4';
            ini_set('date.timezone','Asia/Shanghai');
            $user_ext_data['regis_time'] = date("Y-m-d H:i:s",time());
            $user_ext_data['update_time'] = date("Y-m-d H:i:s",time());
            $user_ext_data['user_id'] = $user_reuslt;
            $user_ext_db = M('UserExt');
            $user_ext_result = $user_ext_db->add($user_ext_data);
            if($user_ext_result){
                return $user_reuslt;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    //获取用户详细资料
    public function getUserInfo($user_id){
        $user_field = array('user_id','user_name','sex','image_url','birthday','address');
        $user_map['user_id'] = array('eq',$user_id);
        $user_result = $this->field($user_field)->where($user_map)->select();
        if(is_array($user_result)){
            foreach($user_result as &$user_result_one){
                $user_ext_field = array('degree_year','degree_school','degree_major','ispublic_phone','ispublic_degree','ispublic_birthday','agent_j','agent_y','agent_j_desc','agent_y_desc','jobs','duration');
                $user_ext_map['user_id'] = array('eq',$user_id);
                $user_ext_db = M('User_ext');
                $user_ext_result = $user_ext_db->field($user_ext_field)->where($user_ext_map)->select();
                //dump($user_ext_result);
                if(is_array($user_ext_result)){
                    $user_result_one['degree_year'] = $user_ext_result[0]['degree_year'];
                    $user_result_one['degree_school'] = $user_ext_result[0]['degree_school'];
                    $user_result_one['degree_major'] = $user_ext_result[0]['degree_major'];
                    $user_result_one['ispublic_phone'] = $user_ext_result[0]['ispublic_phone'];
                    $user_result_one['ispublic_degree'] = $user_ext_result[0]['ispublic_degree'];
                    $user_result_one['ispublic_birthday'] = $user_ext_result[0]['ispublic_birthday'];
                    $user_result_one['agent_j'] = $user_ext_result[0]['agent_j'];
                    $user_result_one['agent_y'] = $user_ext_result[0]['agent_y'];
                    $user_result_one['agent_j_desc'] = $user_ext_result[0]['agent_j_desc'];
                    $user_result_one['agent_y_desc'] = $user_ext_result[0]['agent_y_desc'];
                    $user_result_one['jobs'] = $user_ext_result[0]['jobs'];
                    $user_result_one['duration'] = $user_ext_result[0]['duration'];

                }else{
                    //获取user_ext表信息失败
                }
            }
            return $user_result[0];
        }else{
            return false;
        }
    }

    //修改用户详细资料
    public function setUserInfo($user_data,$user_ext_data,$user_id){

        $user_result = $this->where(array('user_id'=>$user_id))->save($user_data);
        //dump($user_result);
        $user_ext_db = M('UserExt');
        $user_ext_result = $user_ext_db->where(array('user_id'=>$user_id))->save($user_ext_data);
        //dump($user_ext_result);
        $mission_db = M('Mission');
        $mission_data['publisher_name'] = $user_data['user_name'];
        $mission_result = $mission_db->where(array('publisher_id'=>$user_id))->save($mission_data);
        //dump($mission_result);
        if( $user_result || $user_ext_result || $mission_result ){
            return true;
        }else{
            return false;
        }
    }

    //获取用户简历首页信息
    public function getResumeBaseInfo($user_id){
        $user_field = array('user_id','user_name','sex','birthday','address');
        $user_map['user_id'] = array('eq',$user_id);
        $user_result = $this->field($user_field)->where($user_map)->select();
        //dump($user_result);
        if(is_array($user_result)){
            foreach($user_result as &$user_result_one){
                $user_ext_field = array('degree_school','degree_year','degree_major','ispublic_degree','ispublic_birthday','jobs','duration');
                $user_ext_map['user_id'] = array('eq',$user_id);
                $user_ext_db = M('UserExt');
                $user_ext_result = $user_ext_db->field($user_ext_field)->where($user_ext_map)->select();
                //dump($user_ext_result);
                if(is_array($user_ext_result)){
                    $user_result_one['degree_school'] = $user_ext_result[0]['degree_school'];
                    $user_result_one['degree_year'] = $user_ext_result[0]['degree_year'];
                    $user_result_one['degree_major'] = $user_ext_result[0]['degree_major'];
                    $user_result_one['ispublic_degree'] = $user_ext_result[0]['ispublic_degree'];
                    $user_result_one['ispublic_birthday'] = $user_ext_result[0]['ispublic_birthday'];
                    $user_result_one['jobs'] = $user_ext_result[0]['jobs'];
                    $user_result_one['duration'] = $user_ext_result[0]['duration'];
                    if( 1 == $user_ext_result[0]['ispublic_degree']){
                        $user_result_one['degree_school'] = '';
                        $user_result_one['degree_year'] = '';
                        $user_result_one['degree_major'] = '';
                    }
                    if( null ==$user_result_one['birthday'] || 0000-00-00 == $user_result_one['birthday'] ){
                        $user_result_one['birthday'] = '';
                    }else{
                        $user_result_one['birthday'] = date("Y",strtotime($user_result_one['birthday']));
                    }

                    if( 1 == $user_ext_result[0]['ispublic_birthday']){
                        $user_result_one['birthday'] = '';
                    }

                }else{
                    //获取user_ext信息失败
                }
                //获取resume表信息
                $resume_field = array('resume_id','introduction','salary','is_public');
                $resume_map['user_id'] = array('eq',$user_id);
                $resume_db = M('Resume');
                $resume_result = $resume_db->field($resume_field)->where($resume_map)->select();
                //dump($resume_result);
                if(is_array($resume_result)){
                    $user_result_one['resume_id'] = $resume_result[0]['resume_id'];
                    $user_result_one['introduction'] = $resume_result[0]['introduction'];
                    $user_result_one['salary'] = $resume_result[0]['salary'];
                    $user_result_one['is_public'] = $resume_result[0]['is_public'];
                }else{
                    //获取resume信息失败
                }

                //获取experience信息
                $experience_field = array('exp_id','project_name','role','r_order');
                $experience_order = '`r_order` asc';
                $experience_map['resume_id'] = array('eq',$user_result_one['resume_id']);
                $experience_db = M('Experience');
                $experience_result = $experience_db->field($experience_field)->where($experience_map)->order($experience_order)->select();
                if(is_array($experience_result)){
                    $user_result_one['experience'] = $experience_result;
                }else{
                    //获取experience失败
                }

                //获取resume_att表信息
                $resume_att_map['resume_id'] = array('eq',$user_result_one['resume_id']);
                $resume_att_db = M('Resume_att');
                $resume_att_result = $resume_att_db->where($resume_att_map)->select();
                //dump($resume_att_result);
                if(is_array($resume_att_result)){
                    $resume_att_return['resume_att_id'] = $resume_att_result[0]['resume_att_id'];
                    $resume_att_return['resume_id'] = $resume_att_result[0]['resume_id'];
                    $signal = 0;
                    for( $i = 0 ; $i < 8 ; $i++ ){
                        if( $resume_att_result[0]['image_url'.$i] != NULL ){
                            //$resume_att_return['image']['image_url'.$i] = $resume_att_result['image_url'.$i];
                            $resume_att_return['image'][$signal] = array( 'image_url'.$i => $resume_att_result[0]['image_url'.$i]);
                            $signal += 1;
                        }
                    }
                    $resume_att_return['video_url1'] = $resume_att_result[0]['video_url1'];

                    //dump($resume_att_return);
                    $user_result_one['resume_att'] = $resume_att_return;
                }else{
                    //获取resume_att失败
                }

            }
            return $user_result[0];
        }else{
            return false;
        }
    }

    //获取用户简历首页信息 forPC
    public function getResumeBaseInfoPC($user_id){
        $user_field = array('user_id','user_name','sex','birthday','address');
        $user_map['user_id'] = array('eq',$user_id);
        $user_result = $this->field($user_field)->where($user_map)->select();
        //dump($user_result);
        if(is_array($user_result)){
            foreach($user_result as &$user_result_one){
                $user_ext_field = array('degree_school','degree_year','degree_major','ispublic_degree','ispublic_birthday','jobs','duration');
                $user_ext_map['user_id'] = array('eq',$user_id);
                $user_ext_db = M('UserExt');
                $user_ext_result = $user_ext_db->field($user_ext_field)->where($user_ext_map)->select();
                //dump($user_ext_result);
                if(is_array($user_ext_result)){
                    $user_result_one['degree_school'] = $user_ext_result[0]['degree_school'];
                    $user_result_one['degree_year'] = $user_ext_result[0]['degree_year'];
                    $user_result_one['degree_major'] = $user_ext_result[0]['degree_major'];
                    $user_result_one['ispublic_degree'] = $user_ext_result[0]['ispublic_degree'];
                    $user_result_one['ispublic_birthday'] = $user_ext_result[0]['ispublic_birthday'];
                    $user_result_one['jobs'] = $user_ext_result[0]['jobs'];
                    $user_result_one['duration'] = $user_ext_result[0]['duration'];
                    if( 1 == $user_ext_result[0]['ispublic_degree']){
                        $user_result_one['degree_school'] = '';
                        $user_result_one['degree_year'] = '';
                        $user_result_one['degree_major'] = '';
                    }
                    if( 1 == $user_ext_result[0]['ispublic_birthday']){
                        $user_result_one['birthday'] = date("Y",strtotime($user_result_one['birthday']));
                    }

                }else{
                    //获取user_ext信息失败
                }
                //获取resume表信息
                $resume_field = array('resume_id','introduction','salary','is_public');
                $resume_map['user_id'] = array('eq',$user_id);
                $resume_db = M('Resume');
                $resume_result = $resume_db->field($resume_field)->where($resume_map)->select();
                //dump($resume_result);
                if(is_array($resume_result)){
                    $user_result_one['resume_id'] = $resume_result[0]['resume_id'];
                    $user_result_one['introduction'] = $resume_result[0]['introduction'];
                    $user_result_one['salary'] = $resume_result[0]['salary'];
                    $user_result_one['is_public'] = $resume_result[0]['is_public'];
                }else{
                    //获取resume信息失败
                }

                //获取experience信息
                $experience_field = array('exp_id','project_name','role','r_order','description','url');
                $experience_map['resume_id'] = array('eq',$user_result_one['resume_id']);
                $experience_db = M('Experience');
                $experience_result = $experience_db->field($experience_field)->where($experience_map)->order('r_order asc')->select();
                if(is_array($experience_result)){
                    $user_result_one['experience'] = $experience_result;
                }else{
                    //获取experience失败
                }

                //获取resume_att表信息
                $resume_att_map['resume_id'] = array('eq',$user_result_one['resume_id']);
                $resume_att_db = M('Resume_att');
                $resume_att_result = $resume_att_db->where($resume_att_map)->select();
                if(is_array($resume_att_result)){
                    $user_result_one['resume_att'] = $resume_att_result[0];
                }else{
                    //获取resume_att失败
                }

            }
            return $user_result[0];
        }else{
            return false;
        }
    }


    //电话联系
    public function getPhoneInfo($user_id){
        $user_field = array('user_id','user_name','phone');
        $user_map['user_id'] = array('eq',$user_id);
        $user_result = $this->field($user_field)->where($user_map)->select();
        if(is_array($user_result)){
            return $user_result[0];
        }else{
            return false;
        }
    }

    //重置密码
    public function resetPasswd($phone,$passwd){
        $user_map['phone'] = array('eq',$phone);
        $data['passwd'] = $passwd;
        $user_result = $this->where($user_map)->save($data);
        if( 0 == $user_result || $user_result ){
            return true;
        }else{
            return false;
        }
    }

} 